from flask import request

from handle.courseSelect import editCourseScore, selectCourseScore, studentSelect
from publicHandle.publicHandle import returnData, searchUserId


class apiCouSelect():
    def __init__(self, app):
        @app.route("/editScore", methods=["POST"])
        def editScore():
            if searchUserId(request.headers.get("userid")):
                courseId = request.values.get("courseId")
                studentId = request.values.get("studentId")
                score = request.values.get("score")
                return editCourseScore(courseId, studentId, score)
            else:
                return returnData({}, "请先登录")

        @app.route("/allScore", methods=["POST"])
        def allScore():
            if searchUserId(request.headers.get("userid")):
                courseId = request.values.get("courseId")
                return selectCourseScore(courseId)
            else:
                return returnData({}, "请先登录")

        @app.route("/selectCourse", methods=["POST"])
        def selectScore():
            if searchUserId(request.headers.get("userid")):
                courseId = request.values.get("courseId")
                studentIds = request.values.get("studentId")[1:-1].split(',')
                return studentSelect(courseId, studentIds)
            else:
                return returnData({}, "请先登录")
